//bange_layercolor.cpp
//Copyright (C) 2010-2011 Oscar (.teri) Triano

//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include <bange_layercolor.hpp>

bange::layercolor::layercolor(int width, int height, cpSpace *space, lua_State *vm):
bange::layer(space, vm){
	this->type = bange::layer::Color;
	this->AddPoint(0, 0);
	this->AddPoint(width, 0);
	this->AddPoint(width, height);
	this->AddPoint(0, height);
	this->shape = dynamic_cast<sf::Shape *>(this);
}

bool bange::layercolor::NewIndex(lua_State *vm){
	//userdata, key, data
	if (this->bange::layer::NewIndex(vm)){
		return true;}
	if (strcmp("r", lua_tostring(vm, 2)) == 0 && lua_isnumber(vm, 3)){
		sf::Color color = this->GetColor();
		color.r = static_cast<sf::Uint8>(lua_tonumber(vm, 3));
		this->SetColor(color);
		return true;}
	if (strcmp("g", lua_tostring(vm, 2)) == 0 && lua_isnumber(vm, 3)){
		sf::Color color = this->GetColor();
		color.g = static_cast<sf::Uint8>(lua_tonumber(vm, 3));
		this->SetColor(color);
		return true;}
	if (strcmp("b", lua_tostring(vm, 2)) == 0 && lua_isnumber(vm, 3)){
		sf::Color color = this->GetColor();
		color.b = static_cast<sf::Uint8>(lua_tonumber(vm, 3));
		this->SetColor(color);
		return true;}
	if (strcmp("a", lua_tostring(vm, 2)) == 0 && lua_isnumber(vm, 3)){
		sf::Color color = this->GetColor();
		color.a = static_cast<sf::Uint8>(lua_tonumber(vm, 3));
		this->SetColor(color);
		return true;}
	if (strcmp("blendmode", lua_tostring(vm, 2)) == 0 && lua_isnumber(vm, 3)){
		this->SetBlendMode(static_cast<sf::Blend::Mode>(lua_tonumber(vm, 3)));
		return true;}
	return false;
}

bool bange::layercolor::Index(lua_State *vm){
	//userdata, key
	if (this->bange::layer::Index(vm)){
		return true;}
	if (strcmp("r", lua_tostring(vm, 2)) == 0){
		sf::Color color = this->GetColor();
		lua_pushnumber(vm, color.r);
		return true;}
	if (strcmp("g", lua_tostring(vm, 2)) == 0){
		sf::Color color = this->GetColor();
		lua_pushnumber(vm, color.g);
		return true;}
	if (strcmp("b", lua_tostring(vm, 2)) == 0){
		sf::Color color = this->GetColor();
		lua_pushnumber(vm, color.b);
		return true;}
	if (strcmp("a", lua_tostring(vm, 2)) == 0){
		sf::Color color = this->GetColor();
		lua_pushnumber(vm, color.a);
		return true;}
	if (strcmp("blendmode", lua_tostring(vm, 2)) == 0){
		lua_pushnumber(vm, static_cast<lua_Number>(this->GetBlendMode()));
		return true;}
	return false;
}

void bange::layercolor::Draw(sf::RenderWindow *renderwindow, sf::View &currentview){;
	renderwindow->SetView(renderwindow->GetDefaultView());
	renderwindow->Draw(*this->shape);
	renderwindow->SetView(currentview);
}

bange::layercolor::~layercolor(){
	cout << "bange::layercolor destructor for: " << this << endl;
}
